use strict;
use warnings;

main();

sub help {
    print "Usage:\n";
    print "Communication WORK_FOLDER -r i to generate experiment report for robot i\n";
    print "Communication WORK_FOLDER -a start stop to generate experiment report for between robots (start, stop)\n";

    exit(1);
}

sub main {
    help() if not defined $ARGV[0] or not defined $ARGV[1];
    chdir $ARGV[0] . "/data"
        or die "Can't go into $ARGV[0]/data\n";
    
    if($ARGV[1] eq "-r") {
        help() if not defined $ARGV[2];
        GenerateRobotData($ARGV[2]);
    }
    elsif($ARGV[1] eq "-a") {
        help() if not defined $ARGV[2];
        help() if not defined $ARGV[3];
        GenerateAllData($ARGV[2], $ARGV[3]);
    }
    else {
        help();
    }
}

sub GenerateRobotData {
    my $rid = shift;

    print "Processing simulation data from robot " . $rid . "\n";
    open FILE, "Robot_$rid.dat";
    my ( @time, @communication );
    my $lastCommunication = 0;
    foreach(<FILE>) {
        if(/\[Time (\d+)\].*\[Communication (\d+)\].*/) {
            push @time, $1;
            push @communication, $2 - $lastCommunication;
            $lastCommunication = $2;
        }
    }
    close FILE;

    print "Gernerating temp data file for robot " . $rid . "\n";
    open TMP_FILE,">tmp.dat";
    print TMP_FILE $time[$_] . ' ' . $communication[$_] . "\n"
        foreach(0 .. scalar(@time) - 1);

    print "Drawing image for robot " . $rid . "\n";
    system("gnuplot -e \'plot \"tmp.dat\" with line;pause -1\'");
    last;
}

sub GenerateAllData {
    my ( $startId, $endId ) = ( @_ );
    my ( @time, @avgCommunication, @maxCommunication, @minCommunication );

    print "Processing simulation data for robots($startId, $endId)\n";
    foreach my $rid($startId .. $endId) {
        open FILE, "Robot_$rid.dat";
        my ( $line, $lastCommunication ) = ( 0, 0 );
        foreach(<FILE>) {
            if(/\[Time (\d+)\].*\[Communication (\d+)\].*/) {
                my $communication = $2 - $lastCommunication;
                $lastCommunication = $2;

                $time[$line] = $1 if not defined $time[$line];
                $avgCommunication[$line] = defined $avgCommunication[$line] ? $avgCommunication[$line] + $communication / ($endId - $startId)
                    : $communication;
                $maxCommunication[$line] = $communication if not defined $maxCommunication[$line] or $maxCommunication[$line] < $communication;
                $minCommunication[$line] = $communication if not defined $minCommunication[$line] or $minCommunication[$line] > $communication;
                $line++;
            }
        }
        close FILE;
    }

    print "Gernerating temp data file for robots\n";
    open TMP_FILE,">tmp.dat";
    print TMP_FILE $time[$_] . ' ' . $avgCommunication[$_] . ' ' . $maxCommunication[$_] . ' ' . $minCommunication[$_] . "\n"
        foreach (0 .. scalar(@time) - 1);
    close TMP_FILE;

    print "Drawing image for all robots\n";
    system("gnuplot -e \'plot \"tmp.dat\" with line, \"tmp.dat\" using 1:3 with line, \"tmp.dat\" using 1:4 with line;pause -1\'");
}
